<?php
/**
 * Return client's weather
 */
	include_once('object/clients.php');
	include_once('object/weatherObject.php');
	include_once('getWebservice.php');
	
	
	class clientMessage
	{
	    /**
	     * get weather method
	     * @returns a array weathers 
	     */
		public function getUserAddress(){
//			create webservice 
			$basicMessage = new getWebservice();
//			call Ip webservice 
			$param = array();
			$result = $basicMessage->gWebservice('http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?WSDL','getGeoIPContext',$param);
//			format date to object
			$client = new clients();
			$client->clientip = $result['getGeoIPContextResult']['string'][0];
			$client->clientAddress = $result['getGeoIPContextResult']['string'][1];
//			citys list
			$citys = array('北京','上海','天津','重庆','大连');
// 			match citys
			foreach($citys as $var){
				if (preg_match("/".$var."/", $client->clientAddress)) {
				    $city =  $var;
				}
			}
//			call weather webservice
			$param = array('theCityName' => $city);
			$result = $basicMessage->gWebservice('http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL','getWeatherbyCityName',$param);
//			create weather object
			$weathers = new weatherObject();
//			format date to object			
				$weathers->cityName = $result['getWeatherbyCityNameResult']['string'][1];
	//			today
				$weathers->todayTemperature = $result['getWeatherbyCityNameResult']['string'][5];
				$weathers->todayStatus = $result['getWeatherbyCityNameResult']['string'][6];
				$weathers->todayWind = $result['getWeatherbyCityNameResult']['string'][7];
				$weathers->todayIcon = $result['getWeatherbyCityNameResult']['string'][8];
				$weathers->todayIcon2 = $result['getWeatherbyCityNameResult']['string'][9];
				$weathers->todaydepict = $result['getWeatherbyCityNameResult']['string'][10];
	//			tomorrow
				$weathers->tomorrowTemperature = $result['getWeatherbyCityNameResult']['string'][12];
				$weathers->tomorrowStatus = $result['getWeatherbyCityNameResult']['string'][13];
				$weathers->tomorrowWind = $result['getWeatherbyCityNameResult']['string'][14];
				$weathers->tomorrowIcon = $result['getWeatherbyCityNameResult']['string'][15];
				$weathers->tomorrowIcon2 = $result['getWeatherbyCityNameResult']['string'][16];
	//			acquired
				$weathers->acquiredTemperature = $result['getWeatherbyCityNameResult']['string'][17];
				$weathers->acquiredStatus = $result['getWeatherbyCityNameResult']['string'][18];
				$weathers->acquiredWind = $result['getWeatherbyCityNameResult']['string'][19];
				$weathers->acquiredIcon = $result['getWeatherbyCityNameResult']['string'][20];
				$weathers->acquiredIcon2 = $result['getWeatherbyCityNameResult']['string'][21];
			
				$weathers->citydepict = $result['getWeatherbyCityNameResult']['string'][22];
			
			return $weathers;
		}
		/*
		 * return current hours
		 * return number 
		 */
		public function getCurrentHours(){
			$today  = date("H")+8;
			if($today >24){
				return ($today-24);  
			}else{
				return $today; 
			} 
		}
	}
?>
